<?php

/**
 * 图片处理类库
 * @copyright Copyright&copy; 2015, 广州六象元一网络科技有限公司
 * @author 王海龙 $
 * @version  $Id: classImage.php, v 1.0, 2015-01-01 王海龙 $
 */
class classImage
{
    /**
     * 生成缩略图
     * @param   array     $url       源图路径
     * @param   string    $path      目标文件
     * @param   string    $width     目标宽度
     * @param   string    $height    目标高度
     * @param   string    $cut       剪切生成方式
     */

    function thumb($url, $path = 'temp', $width = 400, $height = 400, $cut = '0')
    {
        $this->url = $url;
        $this->resize_width = $width;
        $this->resize_height = $height;
        $this->cut = $cut;
        $this->path = $path;
        // 图片的类型
        $this->type = pathinfo($this->url, PATHINFO_EXTENSION);

        // 初始化图象
        $this->initImg();
        // 生成图象
        $this->newImg();
        ImageDestroy ($this->img);
    }
    function newImg()
    {
        // 源图像宽高
        $this->width = imagesx($this->img);
        $this->height = imagesy($this->img);
        // 改变后的图象的比例
        $resize_ratio = ($this->resize_width)/($this->resize_height);
        // 实际图象的比例
        $ratio = ($this->width)/($this->height);
        if ($this->cut == "1")
        // 裁图
        {
            if ($ratio >= $resize_ratio)
            // 高度优先
            {
                $this->newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($this->newimg, $this->img, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height) * $resize_ratio), $this->height);
            }
            if ($ratio < $resize_ratio)
            // 宽度优先
            {
                $this->newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($this->newimg, $this->img, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width) / $resize_ratio));
            }
        }
        else
        // 不裁图
        {
            if ($ratio >= $resize_ratio)
            {
                $this->newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
                imagecopyresampled($this->newimg, $this->img, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
            }
            if ($ratio < $resize_ratio)
            {
                $this->newimg = imagecreatetruecolor(($this->resize_height) * $ratio, $this->resize_height);
                imagecopyresampled($this->newimg, $this->img, 0, 0, 0, 0, ($this->resize_height) * $ratio, $this->resize_height, $this->width, $this->height);
            }
        }
        $this->outImg();
    }
    //初始化图象
    function initImg()
    {
        if($this->type=="jpg")
        {
            $this->img = imagecreatefromjpeg($this->url);
        }
        if($this->type=="gif")
        {
            $this->img = imagecreatefromgif($this->url);
        }
        if($this->type=="png")
        {
            $this->img = imagecreatefrompng($this->url);
        }
    }
    //初始化图象
    function outImg()
    {
        if($this->type=="jpg")
        {
            ImageJpeg($this->newimg,$this->path);
        }
        if($this->type=="gif")
        {
            ImageGif($this->newimg,$this->path);
        }
        if($this->type=="png")
        {
            ImagePng($this->newimg,$this->path);
        }
    }
}